/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.cakerun.service.impl;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.admin.system.model.entity.DeptDO;
import top.continew.admin.system.model.entity.user.UserDO;
import top.continew.admin.system.model.resp.DeptResp;
import top.continew.admin.system.model.resp.user.UserDetailResp;
import top.continew.admin.system.service.DeptService;
import top.continew.admin.system.service.UserService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.admin.cakerun.mapper.BrandAccountMapper;
import top.continew.admin.cakerun.model.entity.BrandAccountDO;
import top.continew.admin.cakerun.model.query.BrandAccountQuery;
import top.continew.admin.cakerun.model.req.BrandAccountReq;
import top.continew.admin.cakerun.model.resp.BrandAccountDetailResp;
import top.continew.admin.cakerun.model.resp.BrandAccountResp;
import top.continew.admin.cakerun.service.BrandAccountService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 美团账号业务实现
 *
 * @author tangyu
 * @since 2025/07/02 23:41
 */
@Service
@RequiredArgsConstructor
public class BrandAccountServiceImpl extends BaseServiceImpl<BrandAccountMapper, BrandAccountDO, BrandAccountResp, BrandAccountDetailResp, BrandAccountQuery, BrandAccountReq> implements BrandAccountService {
    private final UserService userService;
    private final DeptService deptService;

    @Override
    public List<BrandAccountDO> findByIds(List<Integer> ids) {
        return this.listByIds(ids);
    }

    @Override
    public Long create(BrandAccountReq req) {
        if (req.getUserId() != null) {
            UserDetailResp userDetailResp = this.userService.get(req.getUserId());
            if (userDetailResp != null) {
                req.setDeptId(userDetailResp.getDeptId());
            }
        }
        return super.create(req);
    }

    @Override
    public void update(BrandAccountReq req, Long id) {
        if (req.getUserId() != null) {
            UserDetailResp userDetailResp = this.userService.get(req.getUserId());
            if (userDetailResp != null) {
                req.setDeptId(userDetailResp.getDeptId());
            }
        }
        super.update(req, id);
    }

    @Override
    public PageResp<BrandAccountResp> page(BrandAccountQuery query, PageQuery pageQuery) {
        PageResp<BrandAccountResp> page = super.page(query, pageQuery);
        List<BrandAccountResp> list = page.getList();
        List<String> deptIds = list.stream().map(BrandAccountResp::getDeptId).distinct().toList();
        List<String> userIds = list.stream().map(BrandAccountResp::getUserId).distinct().toList();
        List<DeptDO> deptList = this.deptService.lambdaQuery().in(DeptDO::getId, deptIds).list();
        Map<String, String> deptIdAndName = deptList.stream()
            .collect(Collectors.toMap(deptDO -> String.valueOf(deptDO.getId()), DeptDO::getName, (k1, k2) -> k2));
        List<UserDO> userList = this.userService.lambdaQuery().in(UserDO::getId, userIds).list();
        Map<String, String> userIdAndnickname = userList.stream()
            .collect(Collectors.toMap(user -> String.valueOf(user.getId()), UserDO::getNickname, (k1, k2) -> k2));
        list.forEach(brandAccountResp -> {
            brandAccountResp.setDeptName(deptIdAndName.get(brandAccountResp.getDeptId()));
            brandAccountResp.setNickname(userIdAndnickname.get(brandAccountResp.getUserId()));
        });
        return page;
    }

    @Override
    public BrandAccountDetailResp get(Long id) {
        BrandAccountDetailResp brandAccountDetailResp = super.get(id);
        if (brandAccountDetailResp.getDeptId() != null) {
            DeptResp deptResp = this.deptService.get(brandAccountDetailResp.getDeptId());
            if (deptResp != null) {
                brandAccountDetailResp.setDeptName(deptResp.getName());
            }
        }
        if (brandAccountDetailResp.getUserId() != null) {
            UserDetailResp userDetailResp = this.userService.get(brandAccountDetailResp.getUserId());
            if (userDetailResp != null) {
                brandAccountDetailResp.setNickname(userDetailResp.getNickname());
            }
        }
        return brandAccountDetailResp;
    }
}